package com.cloud.force.interfaces;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloud.force.api.bean.Part;
import com.cloud.force.api.bean.ProcDef;
import com.cloud.force.common.config.FeignConfiguration;

import feign.hystrix.FallbackFactory;

@FeignClient(value = "service-freeflow", configuration = FeignConfiguration.class, fallbackFactory = FeignClientFreeFlowFallbackFactory.class)
public interface FeignClientFreeflow {
	@RequestMapping(value = "freeflow/interface/listprocbybiz", method = RequestMethod.GET)
	public List<ProcDef> listProcDefByBizId(@RequestParam("bizId") long bizId);

	@RequestMapping(value = "freeflow/interface/loadpartsbyactdef", method = RequestMethod.GET)
	public List<Part> loadPartsByActDef(@RequestParam("actDefId") long actDefId);

	@RequestMapping(value = "freeflow/interface/loadpartsbyprocdef", method = RequestMethod.GET)
	public List<Part> loadPartsByProcDef(@RequestParam("procDefId") long procDefId);

	@RequestMapping(value = "freeflow/interface/loadprocactparts", method = RequestMethod.GET)
	public List<ProcDef> loadProcActParts(int bizId);

}

@Component
class FeignClientFreeFlowFallbackFactory implements FallbackFactory<FeignClientFreeflow> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFreeFlowFallbackFactory.class);

	@Override
	public FeignClientFreeflow create(Throwable cause) {
		return new FeignClientFreeflow() {
			@Override
			public List<ProcDef> listProcDefByBizId(long bizId) {
				if (FeignClientFreeFlowFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientFreeFlowFallbackFactory.LOGGER
							.debug("listProcDefByBizCode fallback; reason was:" + cause);
				}
				List<ProcDef> def = new ArrayList<ProcDef>();
				def.add(new ProcDef(1, "简易程序"));
				def.add(new ProcDef(2, "一般程序"));
				return def;
			}

			@Override
			public List<Part> loadPartsByActDef(long actDefId) {
				if (FeignClientFreeFlowFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientFreeFlowFallbackFactory.LOGGER.debug("loadPartsByActDef fallback; reason was:" + cause);
				}
				List<Part> parts = new ArrayList<Part>();
				parts.add(new Part(9999, "执法人员", "岗位", 1, "", "综合执法局"));
				return parts;
			}

			@Override
			public List<Part> loadPartsByProcDef(long procDefId) {
				if (FeignClientFreeFlowFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientFreeFlowFallbackFactory.LOGGER.debug("loadPartsByProcDef fallback; reason was:" + cause);
				}
				List<Part> parts = new ArrayList<Part>();
				parts.add(new Part(0, "执法人员", "人员", 0, "", "综合执法局"));
				return parts;
			}

			@Override
			public List<ProcDef> loadProcActParts(int bizId) {
				return new ArrayList<ProcDef>();
			}
		};
	}

}
